package com.lostreseditores.sabersai.presentation.backingBeans;

import java.util.List;
import java.util.TimeZone;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import com.lostreseditores.sabersai.exceptions.ZMessManager;
import com.lostreseditores.sabersai.model.Bases;
import com.lostreseditores.sabersai.model.dto.BasesDTO;
import com.lostreseditores.sabersai.presentation.businessDelegate.BusinessDelegatorView;
import com.lostreseditores.sabersai.utilities.FacesUtils;

public class BasesView {
    private InputText txtBcompetenciafinn;
    private InputText txtBcompetenciainin;
    private InputText txtBcomponentefinn;
    private InputText txtBcomponenteinin;
    private InputText txtBdescripcionv;
    private InputText txtBdiferencian;
    private InputText txtBpromediofinn;
    private InputText txtBpromedioinin;
    private InputText txtBtotalfinn;
    private InputText txtBtotalinin;
    private InputText txtBaseidn;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<BasesDTO> data;
    private BasesDTO selectedBases;

    public BasesView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            BasesDTO basesDTO = (BasesDTO) e.getObject();

            if (txtBcompetenciafinn == null) {
                txtBcompetenciafinn = new InputText();
            }

            txtBcompetenciafinn.setValue(basesDTO.getBcompetenciafinn());

            if (txtBcompetenciainin == null) {
                txtBcompetenciainin = new InputText();
            }

            txtBcompetenciainin.setValue(basesDTO.getBcompetenciainin());

            if (txtBcomponentefinn == null) {
                txtBcomponentefinn = new InputText();
            }

            txtBcomponentefinn.setValue(basesDTO.getBcomponentefinn());

            if (txtBcomponenteinin == null) {
                txtBcomponenteinin = new InputText();
            }

            txtBcomponenteinin.setValue(basesDTO.getBcomponenteinin());

            if (txtBdescripcionv == null) {
                txtBdescripcionv = new InputText();
            }

            txtBdescripcionv.setValue(basesDTO.getBdescripcionv());

            if (txtBdiferencian == null) {
                txtBdiferencian = new InputText();
            }

            txtBdiferencian.setValue(basesDTO.getBdiferencian());

            if (txtBpromediofinn == null) {
                txtBpromediofinn = new InputText();
            }

            txtBpromediofinn.setValue(basesDTO.getBpromediofinn());

            if (txtBpromedioinin == null) {
                txtBpromedioinin = new InputText();
            }

            txtBpromedioinin.setValue(basesDTO.getBpromedioinin());

            if (txtBtotalfinn == null) {
                txtBtotalfinn = new InputText();
            }

            txtBtotalfinn.setValue(basesDTO.getBtotalfinn());

            if (txtBtotalinin == null) {
                txtBtotalinin = new InputText();
            }

            txtBtotalinin.setValue(basesDTO.getBtotalinin());

            if (txtBaseidn == null) {
                txtBaseidn = new InputText();
            }

            txtBaseidn.setValue(basesDTO.getBaseidn());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtBcompetenciafinn != null) {
            txtBcompetenciafinn.setValue(null);
            txtBcompetenciafinn.setDisabled(true);
        }

        if (txtBcompetenciainin != null) {
            txtBcompetenciainin.setValue(null);
            txtBcompetenciainin.setDisabled(true);
        }

        if (txtBcomponentefinn != null) {
            txtBcomponentefinn.setValue(null);
            txtBcomponentefinn.setDisabled(true);
        }

        if (txtBcomponenteinin != null) {
            txtBcomponenteinin.setValue(null);
            txtBcomponenteinin.setDisabled(true);
        }

        if (txtBdescripcionv != null) {
            txtBdescripcionv.setValue(null);
            txtBdescripcionv.setDisabled(true);
        }

        if (txtBdiferencian != null) {
            txtBdiferencian.setValue(null);
            txtBdiferencian.setDisabled(true);
        }

        if (txtBpromediofinn != null) {
            txtBpromediofinn.setValue(null);
            txtBpromediofinn.setDisabled(true);
        }

        if (txtBpromedioinin != null) {
            txtBpromedioinin.setValue(null);
            txtBpromedioinin.setDisabled(true);
        }

        if (txtBtotalfinn != null) {
            txtBtotalfinn.setValue(null);
            txtBtotalfinn.setDisabled(true);
        }

        if (txtBtotalinin != null) {
            txtBtotalinin.setValue(null);
            txtBtotalinin.setDisabled(true);
        }

        if (txtBaseidn != null) {
            txtBaseidn.setValue(null);
            txtBaseidn.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Bases entity = null;

        try {
            Integer baseidn = new Integer(txtBaseidn.getValue().toString());
            entity = BusinessDelegatorView.getBases(baseidn);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtBcompetenciafinn.setDisabled(false);
            txtBcompetenciainin.setDisabled(false);
            txtBcomponentefinn.setDisabled(false);
            txtBcomponenteinin.setDisabled(false);
            txtBdescripcionv.setDisabled(false);
            txtBdiferencian.setDisabled(false);
            txtBpromediofinn.setDisabled(false);
            txtBpromedioinin.setDisabled(false);
            txtBtotalfinn.setDisabled(false);
            txtBtotalinin.setDisabled(false);
            txtBaseidn.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtBcompetenciafinn.setValue(entity.getBcompetenciafinn());
            txtBcompetenciafinn.setDisabled(false);
            txtBcompetenciainin.setValue(entity.getBcompetenciainin());
            txtBcompetenciainin.setDisabled(false);
            txtBcomponentefinn.setValue(entity.getBcomponentefinn());
            txtBcomponentefinn.setDisabled(false);
            txtBcomponenteinin.setValue(entity.getBcomponenteinin());
            txtBcomponenteinin.setDisabled(false);
            txtBdescripcionv.setValue(entity.getBdescripcionv());
            txtBdescripcionv.setDisabled(false);
            txtBdiferencian.setValue(entity.getBdiferencian());
            txtBdiferencian.setDisabled(false);
            txtBpromediofinn.setValue(entity.getBpromediofinn());
            txtBpromediofinn.setDisabled(false);
            txtBpromedioinin.setValue(entity.getBpromedioinin());
            txtBpromedioinin.setDisabled(false);
            txtBtotalfinn.setValue(entity.getBtotalfinn());
            txtBtotalfinn.setDisabled(false);
            txtBtotalinin.setValue(entity.getBtotalinin());
            txtBtotalinin.setDisabled(false);
            txtBaseidn.setValue(entity.getBaseidn());
            txtBaseidn.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveBases(FacesUtils.checkInteger(txtBaseidn),
                FacesUtils.checkLong(txtBcompetenciafinn),
                FacesUtils.checkLong(txtBcompetenciainin),
                FacesUtils.checkLong(txtBcomponentefinn),
                FacesUtils.checkLong(txtBcomponenteinin),
                FacesUtils.checkString(txtBdescripcionv),
                FacesUtils.checkLong(txtBdiferencian),
                FacesUtils.checkLong(txtBpromediofinn),
                FacesUtils.checkLong(txtBpromedioinin),
                FacesUtils.checkLong(txtBtotalfinn),
                FacesUtils.checkLong(txtBtotalinin));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteBases(FacesUtils.checkInteger(
                    txtBaseidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateBases(FacesUtils.checkInteger(
                    txtBaseidn), FacesUtils.checkLong(txtBcompetenciafinn),
                FacesUtils.checkLong(txtBcompetenciainin),
                FacesUtils.checkLong(txtBcomponentefinn),
                FacesUtils.checkLong(txtBcomponenteinin),
                FacesUtils.checkString(txtBdescripcionv),
                FacesUtils.checkLong(txtBdiferencian),
                FacesUtils.checkLong(txtBpromediofinn),
                FacesUtils.checkLong(txtBpromedioinin),
                FacesUtils.checkLong(txtBtotalfinn),
                FacesUtils.checkLong(txtBtotalinin));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtBaseidn == null) {
                txtBaseidn = new InputText();
            }

            txtBaseidn.setValue(selectedBases.getBaseidn());

            BusinessDelegatorView.deleteBases(FacesUtils.checkInteger(
                    txtBaseidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedBases);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(Integer baseidn, Long bcompetenciafinn,
        Long bcompetenciainin, Long bcomponentefinn, Long bcomponenteinin,
        String bdescripcionv, Long bdiferencian, Long bpromediofinn,
        Long bpromedioinin, Long btotalfinn, Long btotalinin)
        throws Exception {
        try {
            BusinessDelegatorView.updateBases(baseidn, bcompetenciafinn,
                bcompetenciainin, bcomponentefinn, bcomponenteinin,
                bdescripcionv, bdiferencian, bpromediofinn, bpromedioinin,
                btotalfinn, btotalinin);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("BasesView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtBcompetenciafinn() {
        return txtBcompetenciafinn;
    }

    public void setTxtBcompetenciafinn(InputText txtBcompetenciafinn) {
        this.txtBcompetenciafinn = txtBcompetenciafinn;
    }

    public InputText getTxtBcompetenciainin() {
        return txtBcompetenciainin;
    }

    public void setTxtBcompetenciainin(InputText txtBcompetenciainin) {
        this.txtBcompetenciainin = txtBcompetenciainin;
    }

    public InputText getTxtBcomponentefinn() {
        return txtBcomponentefinn;
    }

    public void setTxtBcomponentefinn(InputText txtBcomponentefinn) {
        this.txtBcomponentefinn = txtBcomponentefinn;
    }

    public InputText getTxtBcomponenteinin() {
        return txtBcomponenteinin;
    }

    public void setTxtBcomponenteinin(InputText txtBcomponenteinin) {
        this.txtBcomponenteinin = txtBcomponenteinin;
    }

    public InputText getTxtBdescripcionv() {
        return txtBdescripcionv;
    }

    public void setTxtBdescripcionv(InputText txtBdescripcionv) {
        this.txtBdescripcionv = txtBdescripcionv;
    }

    public InputText getTxtBdiferencian() {
        return txtBdiferencian;
    }

    public void setTxtBdiferencian(InputText txtBdiferencian) {
        this.txtBdiferencian = txtBdiferencian;
    }

    public InputText getTxtBpromediofinn() {
        return txtBpromediofinn;
    }

    public void setTxtBpromediofinn(InputText txtBpromediofinn) {
        this.txtBpromediofinn = txtBpromediofinn;
    }

    public InputText getTxtBpromedioinin() {
        return txtBpromedioinin;
    }

    public void setTxtBpromedioinin(InputText txtBpromedioinin) {
        this.txtBpromedioinin = txtBpromedioinin;
    }

    public InputText getTxtBtotalfinn() {
        return txtBtotalfinn;
    }

    public void setTxtBtotalfinn(InputText txtBtotalfinn) {
        this.txtBtotalfinn = txtBtotalfinn;
    }

    public InputText getTxtBtotalinin() {
        return txtBtotalinin;
    }

    public void setTxtBtotalinin(InputText txtBtotalinin) {
        this.txtBtotalinin = txtBtotalinin;
    }

    public InputText getTxtBaseidn() {
        return txtBaseidn;
    }

    public void setTxtBaseidn(InputText txtBaseidn) {
        this.txtBaseidn = txtBaseidn;
    }

    public List<BasesDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataBases();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<BasesDTO> basesDTO) {
        this.data = basesDTO;
    }

    public BasesDTO getSelectedBases() {
        return selectedBases;
    }

    public void setSelectedBases(BasesDTO bases) {
        this.selectedBases = bases;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
